package com.cqut.forum.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.PictureGroup;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.PictureGroupService;
import com.cqut.forum.service.PictureReplyService;
import com.cqut.forum.service.PictureService;

/**
 * 图片分组action
 * @author yangxun
 */
@Controller
public class PictureGroupAction {
	
	@Resource
	private PictureGroupService pgService;
	@Resource
	private PictureService pictureService;
	@Resource
	private PictureReplyService prService;
	
	@RequestMapping("/picture/create_group")
	@ResponseBody
	public Map<String,Object> createPictureGroup(HttpServletRequest request,
			String pgName,String isPublic,String password){
		PictureGroup pg = new PictureGroup();
		pg.setIsPublic(isPublic.equals("公开")?true:false);
		pg.setPictureGroupName(pgName);
		pg.setPictureNumber(0);
		pg.setOwnerId(((User)request.getSession().getAttribute("user")).getUserId());
		pg.setPicturePassword(password);
		
		pg = pgService.insert(pg);
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("groupId", pg.getPictureGroupId());
		result.put("image","../../images/left1.jpg");
		result.put("name", pg.getPictureGroupName());
		result.put("number", pg.getPictureNumber()+"张");
		result.put("ispublic", pg.getIsPublic()?"公开":"加密");
		return result;
	}
	
	@RequestMapping("/picture/check_group")
	@ResponseBody
	public Map<String, Object> checkGroup(HttpServletRequest request,String pgName){
		User user = (User)request.getSession().getAttribute("user");
		Map<String, Object> result = new HashMap<String, Object>();
		if(pgName == ""){
			result.put("value", "名称无效");
			return result;
		}
		if(pgService.exist(user.getUserId(),pgName)){
			result.put("value", "可以使用");
		}
		else{
			result.put("value", "已存在该名称");
		}
		return result;
	}
	
	@RequestMapping("/picture/get_groups")
	@ResponseBody
	public List<Map<String, Object>> getGroups(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		
		return loadGroups(user.getUserId());
	}
	
	@RequestMapping("/picture/system_show_groups")
	@ResponseBody
	public List<Map<String, Object>> loadSystemGroup(){
		List<Map<String, Object>> groups = pgService.getShowGroups();
		return groups;
	}
	
	public List<Map<String, Object>> loadGroups(int userId){
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		List<PictureGroup> groups = pgService.getGroups(userId);
		
		for(PictureGroup pg:groups){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("groupId", pg.getPictureGroupId());
			m.put("image", pg.getMarkPictureUrl());
			m.put("name", pg.getPictureGroupName());
			m.put("number", pg.getPictureNumber()+"张");
			m.put("ispublic", pg.getIsPublic()?"公开":"加密");
			m.put("password", pg.getPicturePassword());
			result.add(m);
		}
		return result;
	}
	
	@RequestMapping("/pictureGroup/delete_group")
	@ResponseBody
	public String deleteGroup(HttpServletRequest request,int[] groupIds){
		for(int i =0;i<groupIds.length;i++){
			int groupId = groupIds[i];
			PictureGroup pg = pgService.getGroupById(groupId);
			if(pg.getPictureNumber()==0){
				pgService.delete(groupId);
			}
			else{
				List<Map<String, Object>> pictures = pictureService.getByGroupId(groupId, 0, 1000, "picture_id", true);
				for(int j=0;j<pictures.size();j++){
					int pictureId = (Integer)pictures.get(i).get("picture_id");
					List<Map<String, Object>> replys = prService.getPictureReplys(pictureId);
					for(int k=0;k<replys.size();k++){
						int replyId = (Integer)replys.get(i).get("replyId");
						prService.deleteById(replyId);
					}
					pictureService.deleteById(pictureId);
				}
				pgService.delete(groupId);
			}
		}
		return "true";
	}
}
